#!/bin/sh

#DEBHELPER#

[ -x /etc/init.d/udev ] && /etc/init.d/udev restart

# setup emcweb var files
rm -rf /var/cache/linuxcnc/www
mkdir -p /var/cache/linuxcnc/www/data
chmod a+rw /var/cache/linuxcnc/www/data

# add symbolic links to static files
cd /usr/share/linuxcnc/doc-root
find -type d -exec mkdir --parents -- /var/cache/linuxcnc/www/{} \;
find -type f -exec ln -s -- /usr/share/linuxcnc/doc-root/{} /var/cache/linuxcnc/www/{} \;

# if it doesnt exist, create /var/log/linuxcnc.log and make it publically readable
touch /var/log/linuxcnc.log
chmod ugo+r /var/log/linuxcnc.log

# restart the rsyslogd to start logging to /var/log/linuxcnc.conf 
invoke-rc.d rsyslog restart

# The rebranding from linuxcnc to machinekit is taken care of in src/configure for RIPs
# /usr builds to package need it in postinst script
# This  bit should not be needed, just belt and braces :)
# Checking for / creating ~/machinekit dir now in linuxcnc script

if [ -f "/usr/bin/linuxcnc" ]; then
    if [ ! -f "/usr/bin/machinekit" ]; then
	if [ ! -L "/usr/bin/machinekit" ]; then
	    ln -s /usr/bin/linuxcnc /usr/bin/machinekit
	    echo "Creating machinekit symlink"
	fi
    fi
else
    if [ -f "/usr/local/bin/linuxcnc" ]; then
        if [ ! -f "/usr/local/bin/machinekit" ]; then
	    if [ ! -L "/usr/local/bin/machinekit" ]; then
		ln -s /usr/local/bin/linuxcnc /usr/local/bin/machinekit
		echo "Creating machinekit symlink"
	    fi
        fi
    fi
fi

# Until rebranded completely solve corner cases like this

if [ -f "/usr/share/linuxcnc" ]; then
    if [ ! -f "/usr/share/machinekit" ]; then
	if [ ! -L "/usr/share/machinekit" ]; then
	    ln -s /usr/share/linuxcnc /usr/share/machinekit
	    echo "Creating machinekit symlink"
	fi
    fi
fi


